<?php

namespace app\teacher\model;

use think\Model;
use app\common\model\CourseOrder as CourseOrderModel;

class CourseOrder extends CourseOrderModel
{
    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'user_id')->field('user_id, nickname, avatar, mobile');
    }

    /**
     * 获取用户订单列表
     */
    public static function getUserOrders($teacherId, $status, $size = 20)
    {
        $teacherCourseId = TeacherCourse::getColumnByTeacherId($teacherId, 'teacher_course_id');
        $orderStatus = self::orderStatus($status);
        return self::with(['user'])->whereIn('teacher_course_id', $teacherCourseId)->whereIn('status', $orderStatus)->order('createtime desc')->paginate($size);
    }


    /**
     * 订单状态
     */
    private static function orderStatus($status)
    {
        switch ($status) {
            //待支付
            case 1:
                return [1];
                break;
            //待上课
            case 2:
                return [2];
                break;
            //已完成
            case 3:
                return [3];
                break;
            //取消、退款
            case 4:
                return [0, 4, 5, 6];
                break;
            //全部
            default:
                return [0, 1, 2, 3, 4, 5, 6];
                break;
        }
    }

    /**
     * 订单详情
     */
    public static function getByOrderId($courseOrderId)
    {
        return self::with(['teacherCourse' => function ($query) {
            $query->with(['language'])->withCount('course');
        }, 'course', 'user'])->find($courseOrderId);
    }

    /**
     * 累积订单
     */
    public static function getFinishCount($teacherCourseId)
    {
        return self::whereIn('teacher_course_id', $teacherCourseId)->where('status', 3)->count();
    }
}
